﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Beetle.Sl.EditorControl.Core;
using Beetle.Sl.EditorControl.Controls;

namespace Beetle.Sl.EditorControl.ToolBars.SetFontSize
{
    [ToolBarItemAttribute]
    public class SetFontSizeToolBar : ToolBarItem
    {
        public const string COMMANDTEXT = "SetFontStyle";
        private TextBlock txtFontSize = null;
        private UIElement _content;
        private UIElement _popupContent;
        private double fontSizeTemp = 0;

        public override string Name
        {
            get
            {
                return "SetFontSize";
            }
        }

        public override ToolBarItemVisualTypeEnum ToolBarItemVisualType
        {
            get
            {
                return ToolBarItemVisualTypeEnum.DropDownButton;
            }
        }

        public override UIElement Content
        {
            get
            {
                if (this._content == null)
                {
                    Border border = new Border();
                    border.Margin = new Thickness(1);
                    border.Background = new SolidColorBrush(Colors.White);

                    this.txtFontSize = new TextBlock();
                    this.txtFontSize.Width = 40;
                    this.txtFontSize.VerticalAlignment = VerticalAlignment.Center;
                    this.txtFontSize.Margin = new Thickness(8, 0, 0, 0);
                    border.Child = this.txtFontSize;

                    this._content = border;
                }

                return this._content;
            }
        }

        public override UIElement PopupContent
        {
            get
            {
                if (this._popupContent == null)
                {
                    FontSizeSelectPanel fontSizeSelectPanel = new FontSizeSelectPanel();
                    fontSizeSelectPanel.SelectedValue += new EventHandler(fontSizeSelectPanel_OnSelectedValue);
                    Border border = new Border();
                    border.MaxHeight = 200;
                    border.MinWidth = 140;
                    border.Child = fontSizeSelectPanel;
                    this._popupContent = border;
                }

                return this._popupContent;
            }
        }

        void fontSizeSelectPanel_OnSelectedValue(object sender, EventArgs e)
        {

            this.IsOpenPopup = false;
            this.IsSelected = false;
            FontSizeSelectPanel fontSizeSelectPanel = sender as FontSizeSelectPanel;
            this.txtFontSize.Text = fontSizeSelectPanel.Value.ToString();
            this.fontSizeTemp = fontSizeSelectPanel.Value;

            this.Editor.ExcuteCommand(SetFontSizeToolBar.COMMANDTEXT, new object[] { Run.FontSizeProperty, fontSizeSelectPanel.Value });
        }

        public override void OnCommand(object arg)
        {
            this.Editor.ExcuteCommand(SetFontSizeToolBar.COMMANDTEXT, new object[] { Run.FontSizeProperty, this.fontSizeTemp });
        }

        public override void UpdateVisual()
        {
            if (this.Editor.RichTextBoxInst.IsReadOnly)
            {
                this.IsSelected = false;
                this.IsEnabled = false;
                return;
            }
            else
            {
                this.IsEnabled = true;
            }
        }
    }
}
